﻿using Base.Api.Models;
using Base.Api.Service;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Threading.Tasks;

namespace Base.Api.Controllers
{
    [Route("api/Token")]
    public class TokenController : Controller
    {
        private readonly TokenService tokenService;

        public TokenController(TokenService token)
        {
            this.tokenService = token;
        }

        [HttpPost, Route("Token")]
        public IActionResult GenerateToken([FromBody] LoginTokenRequest request)
        {
            var user = this.tokenService.GetToken(request);
            if (user != null)
            {
                return Ok(new
                {
                    access_token = new JwtSecurityTokenHandler().WriteToken(user.Token),
                    token_type = "Bearer",
                    auth_time = user.AuthTime?.ToString("u"),
                    expires = user.ExpireTime?.ToString("u"),
                    org_id = user.OrgId,
                    member_id = user.MemberId,
                    user_id = user.UserId,
                    user_code = user.UserCode,
                    member_name = user.MemberName,
                    member_code = user.MemberCode,
                    area_id = user.AreaId,
                    iqc_power_level = user.IQCPowerLevel,
                    type = user.Type,
                    members = user.LoginTokenMembers,
                });
            }

            return Ok(new { error = "unsupported_grant_type", error_code = "-1" });
        }

        [HttpPost, Route("LightAppToken")]
        public IActionResult LightAppToken([FromBody] LoginLightAppTokenRequest request)
        {
            var user = this.tokenService.GetLightAppToken(request);
            if (user != null)
            {
                return Ok(new
                {
                    access_token = new JwtSecurityTokenHandler().WriteToken(user.LoginToken.Token),
                    token_type = "Bearer",
                    auth_time = user.LoginToken.AuthTime?.ToString("u"),
                    expires = user.LoginToken.ExpireTime?.ToString("u"),
                    org_id = user.LoginToken.OrgId,
                    member_id = user.LoginToken.MemberId,
                    user_id = user.LoginToken.UserId,
                    user_code = user.LoginToken.UserCode,
                    member_name = user.LoginToken.MemberName,
                    member_code = user.LoginToken.MemberCode,
                    area_id = user.LoginToken.AreaId,
                    iqc_power_level = user.LoginToken.IQCPowerLevel,
                    type = user.LoginToken.Type,
                    members = user.LoginToken.LoginTokenMembers,
                    api_key = user.ApiKey,
                });
            }

            return Ok(new { error = "unsupported_grant_type", error_code = "-1" });
        }
    }
}
